<?php 
/**
 * This file is part of DomAr.
 * 
 * DomAr is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomAr is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomAr.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
* @package DomAr
* @subpackage Plugin
*/
class DomArMedia extends DomArImage {
	
	/**
	* This method is not supported for SWF and will return false for swf files.
	*/
	public function cut( $width, $height, array $settings = array() ) {
		if( in_array($this->getExt(), $this->getSupportedImageExtensions() ))
			return parent::imageCut( $width, $height, $settings );
		return false;
	}
	
	/**
	* This method is not supported for SWF.
	*//**
	* This method is not supported for SWF and will return false for swf files.
	*/
	public function resize( $width, $height, $inSize = true ) {
		if( in_array($this->getExt(), $this->getSupportedImageExtensions() ))
			return parent::resize( $width, $height, $inSize );
		return false;
	}
	
	/**
	* This method is not supported for SWF and will return false for swf files.
	*/
	public function watermark( $watermarkPath, array $settings = array() ) {
		if( in_array($this->getExt(), $this->getSupportedImageExtensions() ))
			return parent::watermark(  $watermarkPath, $settings );
		return false;
	}
	
	/**
	* This method is not supported for SWF and will return false for swf files.
	*/
	protected function convertTo( $type, $quality = 100 ) {
		if( in_array($this->getExt(), $this->getSupportedImageExtensions() ))
			return convertTo( $type, $quality );
		return false;
	}
	
	/**
	* Convert image to html
	* 
	* Possible params are:
	* style - add CSS style to item
	* class - add CSS class to item
	* alt - add alt description to img tag.
	* width - media width
	* height - media height
	* link - will be used for flash media as clickTag link.
	* 
	* @param Array $args
	* @return String
	*/
	public function toHtml( array $args = array() ) {

		if( $this->getExt() == 'swf' ) {
			
			$size = getimagesize($this->getPath() );
			if( $size !== false ) {
				$args['width'] = $size[0];
				$args['height'] = $size[1];
				$args['sizes'] = $size[3];
			} else if( $args['width'] && $args['height'] ) {
				$args['sizes'] = 'width="'.$args['width'].'" height="'.$args['height'].'" ';
			} else {
				return 'Media dimensions cannot be calculated!';				
			}

			if( empty($args["link"]) === false ) 
				$link = 'clickTAG='.$args["link"];
			
			return '<object type="application/x-shockwave-flash" data="'.$this->getUrl() . '?' . $link.'" '.$args['sizes'].'><param name="movie" value="'.$this->getUrl() . '?' . $link.'" /><param name="quality" value="high" /><param name="wmode" value="opaque" /></object>
			';	
			/*
			return '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 
			codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" 
			'.$args['sizes'].'> <param name="movie" value="'.$this->getUrl() . '?' . $link.'" />  
			<param name="quality" value="high"/>
			<param name="wmode" value="opaque"/>
			<embed src="'.$this->getUrl() . '?' . $link.'" quality="high" type="application/x-shockwave-flash" pluginspace="http://www.macromedia.com/go/getflashplayer" '.$args['sizes'].' wmode="opaque"></embed></object>';
*/
		} else {
			return parent::toHtml( $args );
		}
	}
	
	
	public function getSupportedMediaExtensions() {
		return array('swf');
	}

}

/**
* @link http://inditel.ee/
* @copyright 2007-2009 Inditel Meedia OÜ
* @author Oliver Leisalu <oliver@inditel.ee>
* @package DomAr
* @subpackage Plugin
*/
class DomArProperty_DomArMedia extends DomArProperty_DomArImage {

	/**
	* @param Array allowed file extensions
	*/
	protected $allowedExtensions = array('jpg','jpeg','gif','png','swf');


	public function load( DomArObject $object, &$propertyValue ) {
		$d = new DomArMedia( $propertyValue, $this->allowedExtensions, $this->root, $this->url, array('beforeadd'=>$this->listeners['beforeadd'],'afteradd'=>$this->listeners['afteradd'])  );
		$d->changeParent( $object, $this );
		return $d;
	}
	
	public function validate( DomArObject $object, &$propertyValue, array &$customParams = array() ) {
		return (is_object($propertyValue) && get_class($propertyValue) == 'DomArMedia' or is_string($propertyValue) or $propertyValue === NULL) ? 0 : 1;
	}
}

?>